﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YJKMaterialStatistic
{
	public class FamilyLegend
	{
		public static string GetLegendByFamilyName(Autodesk.Revit.DB.Document doc, string familyName)
		{
			if (doc == null || string.IsNullOrWhiteSpace(familyName))
			{
				return string.Empty;
			}
			FamilyLegend.GetAllTableName();
			if (FamilyLegend.tableNames == null || FamilyLegend.tableNames.Count <= 0)
			{
				return string.Empty;
			}
			foreach (string tableName in FamilyLegend.tableNames)
			{
				List<DbFamilyObject> list = FamilyLegend.LoadFamilyDatas(doc, tableName);
				if (list != null && list.Count > 0)
				{
					foreach (DbFamilyObject dbFamilyObject in list)
					{
						if (!string.IsNullOrEmpty(dbFamilyObject.Image2D))
						{
							try
							{
								string b = dbFamilyObject.ToString();
								if (familyName == b)
								{
									return dbFamilyObject.Image2D;
								}
							}
							catch (Exception)
							{
							}
						}
					}
				}
			}
			return string.Empty;
		}

		private static void GetAllTableName()
		{
			if (FamilyLegend.tableNames == null || FamilyLegend.tableNames.Count > 0)
			{
				return;
			}
			try
			{
				string arg = Path.Combine(Product.DataLocation, "HYBIMSPACE.db3");
				string connectionString = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", arg);
				SQLiteConnection sqliteConnection = new SQLiteConnection(connectionString);
				sqliteConnection.Open();
				IDataReader reader = new SQLiteCommand("select name from sqlite_master where type='table' order by name", sqliteConnection)
				{
					CommandType = CommandType.Text
				}.ExecuteReader();
				DataTable dataTable = new DataTable();
				dataTable.Load(reader);
				foreach (object obj in dataTable.Rows)
				{
					DataRow dataRow = (DataRow)obj;
					string item = dataRow[0].ToString();
					FamilyLegend.tableNames.Add(item);
				}
				sqliteConnection.Close();
			}
			catch (Exception)
			{
			}
		}

		public static List<DbFamilyObject> LoadFamilyDatas(Autodesk.Revit.DB.Document doc, string tableName)
		{
			List<DbFamilyObject> list = new List<DbFamilyObject>();
			if (doc == null || string.IsNullOrEmpty(tableName))
			{
				return list;
			}
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			List<ListItem> list2 = null;
			try
			{
				list2 = spaceFamilyDataReader.GetAllName(tableName);
				if (list2 == null || list2.Count <= 0)
				{
					return list;
				}
			}
			catch
			{
				return list;
			}
			foreach (ListItem listItem in list2)
			{
				int id = Convert.ToInt32(listItem.Tag);
				DbFamilyObject dbFamilyObject = new DbFamilyObject();
				List<ListItem> infoById = spaceFamilyDataReader.GetInfoById(tableName, id, doc);
				foreach (ListItem item in infoById)
				{
					FamilyLegend.SetValueForFamilyObject(dbFamilyObject, item);
				}
				list.Add(dbFamilyObject);
			}
			return list;
		}

		private static void SetValueForFamilyObject(DbFamilyObject familyObject, ListItem item)
		{
			if (familyObject == null || item == null)
			{
				return;
			}
			if (item.Name == "Id")
			{
				familyObject.Id = Convert.ToInt32(item.Value);
				return;
			}
			if (item.Name == "Name")
			{
				familyObject.Name = item.Value;
				return;
			}
			if (item.Name == "Family")
			{
				familyObject.Family = item.Value;
				return;
			}
			if (item.Name == "Image")
			{
				familyObject.Image = item.Value;
				return;
			}
			if (item.Name == "Image2D")
			{
				familyObject.Image2D = item.Value;
				return;
			}
			if (item.Name == "Legend")
			{
				familyObject.Legend = item.Value;
			}
		}

		private static List<string> tableNames = new List<string>();
	}
}
